package com.ruoyi.energy.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.energy.domain.TcIn;
import com.ruoyi.energy.domain.TcOut;
import com.ruoyi.energy.domain.TcPark;
import com.ruoyi.energy.domain.TcParking;
import com.ruoyi.energy.domain.vo.ParkCarEnterVO;
import com.ruoyi.energy.domain.vo.ParkCarOutVO;
import com.ruoyi.energy.service.IParkingHandlerService;
import com.ruoyi.energy.service.ITcInService;
import com.ruoyi.energy.service.ITcOutService;
import com.ruoyi.energy.service.ITcParkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ParkingHandlerServiceImpl implements IParkingHandlerService {

    @Autowired
    private ITcInService carInService;

    @Autowired
    private ITcOutService carOutService;

    @Autowired
    private ITcParkService parkservice;


    @Override
    public void handlerOutParkMessage(ParkCarOutVO outVO) {

        List<ParkCarOutVO.DataDTO> list = outVO.getData();
        list.forEach(o -> {
            TcOut tc = new TcOut();
            BeanUtils.copyBeanProp(tc, o);
            //更新停车场名称
            TcParking queryPark = new TcParking();
            queryPark.setParkId(tc.getParkId());
            TcPark park = parkservice.selectTcParkByParkId(tc.getParkId());
            if (park != null) {
                tc.setParkingName(park.getParkName());
            }

            TcOut tcOut = carOutService.selectTcOutByAccessInId(tc.getAccessIdIn());
            if (tcOut != null) {
                String id = tcOut.getId();
                BeanUtils.copyBeanProp(tcOut, tc);
                tcOut.setId(id);
                carOutService.updateTcOut(tcOut);
            } else {
                carOutService.insertTcOut(tc);
            }
        });
    }

    @Override
    public void handlerInParkMessage(ParkCarEnterVO enterVO) {
        List<ParkCarEnterVO.DataDTO> list = enterVO.getData();
        list.forEach(o -> {
            TcIn tc = new TcIn();
            BeanUtils.copyBeanProp(tc, o);
            //更新停车场名称
            TcParking queryPark = new TcParking();
            queryPark.setParkId(tc.getParkId());
            TcPark park = parkservice.selectTcParkByParkId(tc.getParkId());
            if (park != null) {
                tc.setParkingName(park.getParkName());
            }
            if (CollectionUtil.isNotEmpty(o.getImagestream())) {
                tc.setImagestream(o.getImagestream().get(0).getStreamdata());
            }
            carInService.insertTcIn(tc);
        });
    }
}
